package com.myjava.managesystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.myjava.managesystem.common.BaseContext;
import com.myjava.managesystem.common.R;
import com.myjava.managesystem.entity.ShoppingCart;
import com.myjava.managesystem.service.DishService;
import com.myjava.managesystem.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;
    @Autowired
    DishService dishService;

    @GetMapping("/list")
    public R<List<ShoppingCart>> getList() {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        return R.success(shoppingCartService.list(wrapper));
    }

    @PostMapping("/add")
    private R<String> add(ShoppingCart shoppingCart) {
        shoppingCart.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        wrapper.eq(ShoppingCart::getName, shoppingCart.getName());
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        shoppingCart.setCreateTime(LocalDateTime.now());
        if (one == null) {
            shoppingCartService.save(shoppingCart);
        } else {
            shoppingCart.setId(one.getId());
            shoppingCart.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(shoppingCart);
        }
        return R.success("修改成功");
    }

    @PostMapping("/sub")
    private R<String> sub(@RequestBody ShoppingCart shoppingCart) {
        shoppingCart.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        wrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId, shoppingCart.getDishId());
        wrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        shoppingCart.setId(one.getId());
        int num = one.getNumber() - 1;
        if(num==0){
            shoppingCartService.removeById(one.getId());
        }
        else{
            shoppingCart.setNumber(num);
            shoppingCartService.updateById(shoppingCart);
        }
        return R.success("修改成功");
    }

    @DeleteMapping("clean")
    public R<String> deleteAll(){
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartService.list();
        Set<Long> ids = new HashSet<>();
        for(ShoppingCart s:list){
            ids.add(s.getId());
        }
        shoppingCartService.removeByIds(ids);
        return R.success("删除成功");
    }
}
